function [heart_rate_SAS] = FUN_adaptiveSpectrumSubtraction_noMAs(data,state)
%FUN_plotDataDCTEnvelope DCT Envelope
%
%% function preparation and preproccess
if nargin < 2
    state = 1;
end

%% Data preparation and preproccess
% sample time and frequency
% generate time and frequency sequence
size = length(data);
sampletime = 0.01;                                  %sample time that unit is second
ts = sampletime;
t = 0:sampletime:(size-1) * sampletime;             %time point
samplefreq = 1/sampletime;                          %sample frequency that unit is Hz
fs = samplefreq;
f = (0:(size-1))/size*samplefreq;                   %frequency point
fd = f/2;
FontSize = 12;   %12~16

%% Figure configuration
% figure get and setting
if state == 1
    figobj = findobj('name','DCT_ENV');
    if isempty(figobj)
        figobj = figure('name','DCT_ENV');
    end
    figobj_ass = findobj('name','SAS');
    if isempty(figobj_ass)
        figobj_ass = figure('name','SAS');
    end
end
%% DCT analysis
dct_data_abs = abs(dct(data));
% dct_move_abs = abs(dct(motion));   %acce DCT

%% Envelop analysis
dct_data_env = FUN_Envelope(fd,dct_data_abs,'spline');
% dct_move_env = FUN_Envelope(fd,dct_move_abs,'spline');
% dct_data_sub = abs(dct_data_env) - abs(dct_move_env);

%% main zone analysis from f_low to f_high
f_low = 0.5;f_high = 4.0;
n_low = f_low*2*size/samplefreq+1;
n_high = f_high*2*size/samplefreq+1;
fm = fd(n_low:n_high);  % frequency main zone
dct_data_main = dct_data_env(n_low:n_high);
% dct_move_main = dct_move_env(n_low:n_high);

%% find peaks in Envelop
[dct_data_pks,dct_data_locs] = findpeaks(dct_data_main);
% [dct_move_pks,dct_move_locs] = findpeaks(dct_move_main);

%% find the three highest peaks in Envelop
% dct_select_three = 3;
% [dct_data_pks,dct_data_index] = sort(dct_data_pks,'descend');
% dct_data_pks = dct_data_pks(1:dct_select_three);
% dct_data_locs = dct_data_locs(dct_data_index(1:dct_select_three));
% [dct_move_pks,dct_move_index] = sort(dct_move_pks,'descend');
% dct_move_pks = dct_move_pks(1:dct_select_three);
% dct_move_locs = dct_move_locs(dct_move_index(1:dct_select_three));
% dct_data_pks_same = [];
% dct_data_loc_same = [];
% for i = 1 : dct_select_three
%     for j  = 1 : dct_select_three
%         if abs(dct_data_locs(i) - dct_move_locs(j))<4   %4 equals 2 beats
%             dct_data_loc_same = dct_data_locs(i);
%             dct_data_pks_same = dct_data_pks(i);
% %             heart_rate_SAS_same = 60 * fd(dct_data_locs(i)+n_low-1)
%         end
%     end
% end

% [locx,index] = max(setdiff(dct_data_pks,dct_data_pks_same));
% dct_data_loc_same = setdiff(dct_data_locs,dct_data_loc_same);
% heart_rate_SAS_same = 60 * fd(dct_data_loc_same(index)+n_low-1)

%% envelop display
if state == 1
    FUN_figureConfig(figobj,1);
    plot(fd,dct_data_abs,'c',fd,dct_data_env,'b--');
    plot(fd(dct_data_locs+n_low-1),dct_data_pks,'bv');
    l = legend('PPG DCT freq.','PPG Envelope');
    l.Box = 'off';
    l.Location = 'northeast';
    l.FontSize = FontSize-2;
%     
%     FUN_figureConfig(figobj,2);
%     plot(fd,dct_move_abs,'c',fd,dct_move_env,'r--');
%     plot(fd(dct_move_locs+n_low-1),dct_move_pks,'rv');
%     l = legend('MAs DCT freq.','MAs Envelope');
%     l.Box = 'off';
%     l.Location = 'northeast';
%     l.FontSize = FontSize-2;
end

%% adaptive spectrum subtraction
% dct_move_adpt_amp = abs( dct_data_main / dct_move_main )*0.8;
% % dct_move_adpt_amp = abs( pinv(dct_move_main)*dct_data_main)*0.8;
% dct_move_adpt = dct_move_main * dct_move_adpt_amp;
% dct_data_sas = dct_data_main-dct_move_adpt;
% dct_data_sas(find(dct_data_sas<0)) = 0;

[pks,locs]  = max(dct_data_main);
heart_rate_SAS = 60 * fm(locs)

%% adaptive spectrum subtraction display
if (heart_rate_SAS < 50) || (heart_rate_SAS > 100)
    FUN_figureConfig(figobj_ass,1);
    plot(fm,dct_data_main,'b',fm,dct_data_main,'r--');
    l = legend( 'PPG', 'MAs');
    l.Box = 'off';
    l.Location = 'northeast';
    l.FontSize = FontSize-2;
    pause
%     FUN_figureConfig(figobj_ass,2);
%     plot(fm,dct_data_sas,'b');
%     title('Discrete Cosine Transform Analysis');
%     l = legend( 'SAS');
%     l.Box = 'off';
%     l.Location = 'northeast';
%     l.FontSize = FontSize-2;
%     plot(fm(locs),pks,'rv');
end

end